from django.shortcuts import render, HttpResponse, redirect
from BASE.interface import get_backend_data as backend


# Create your views here.
class Users:
    __account_html = 'Users/account.html'
    __users_html = "Users/users.html"

    def users_view(self, request):
        session = None
        try:
            session = request.COOKIES["session"]
        except KeyError:
            return redirect("/users/account")
        comic_history = backend.get_comic_data(
            "http://193.112.72.237/Web-comfun/web/index.php?r=commic/index",
            form_data={
                "function": "Showhistory",
                "session": session
            }
        )
        novel_history = backend.get_novels_data(
            "http://193.112.72.237/Web-comfun/web/index.php?r=fiction/index",
            form_data={
                "function": "Showhistory",
                "session": session
            })
        collected_comic = backend.get_comic_data(
            'http://193.112.72.237/Web-comfun/web/index.php?r=commic/index',
            form_data={
                "function": "Showcollect",
                "session": session,
            }
        )
        collected_novel = backend.get_novels_data(
            'http://193.112.72.237/Web-comfun/web/index.php?r=fiction/index',
            form_data={
                "function": "Showcollect",
                "session": session,
            }
        )
        return render(request, self.__users_html, context={
            "comic_history": comic_history,
            "novel_history": novel_history,
            "collected_comic": collected_comic,
            "collected_novel": collected_novel,
        })
        pass

    def account_view(self, request):
        try:
            session = request.COOKIES["session"]
        except KeyError:
            return render(request, self.__account_html)
        else:
            if session:
                return redirect("/users/users/")
        return render(request, self.__account_html)
        pass

    def get_post(self, request):
        print(request.body)
        print(request.POST.get("username"))
        return HttpResponse(request.body)

    pass


users_views = Users()
